package com.china08.yxyapi.api.manage.tec.teaching.zujuan.intellcomponent;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.china08.yxyapi.util.YxyResUtil;

/**
 * 智能组卷按章节生成试卷
 * 
 */
// @Api(tags = { "v50", "智能组卷 按知识点" })
@CrossOrigin
@RestController
public class IntellComponentQuestionListByKnowledgeController extends YxyApiBaseController {
    @Autowired
    private YxyResUtil yxyResUtil;
    @Autowired
    private ElasticQuesService elasticQuesService;

    // @ApiOperation(value = "智能组卷按知识点生成试卷")
    // @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/intell/knowledge/queslist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        // 查询知识点
        List<Integer> knowIds = reqModel.bookContentIdList;
        List<Integer> bookcontentListAll = new ArrayList<Integer>();
        for (Integer bc : knowIds) {
            List<Integer> bookcontentList = yxyResUtil.findTocByTocKnowParent(bc);
            bookcontentListAll.addAll(bookcontentList);
        }
        // 去重
        Set<Integer> knowIdSet = new HashSet<Integer>();
        for (Integer integer : bookcontentListAll) {
            knowIdSet.add(integer);
        }
        List<QuestionModel> questions = new ArrayList<QuestionModel>();
        QuestionModel questionModel = null;
        List<Integer> knowIds2 = new ArrayList<Integer>(knowIdSet);
        // Set<String> quesIdSet=new HashSet<String>();

        // 单选
        Integer danxuan = 0;
        Integer duoxuan = 0;
        Integer tiankong = 0;
        Integer jieda = 0;
        List<ElasticYxyYxyQuesResource> elasticYxyYxyQuesResourceLst = elasticQuesService
                .findQuesListByChapter(new String[] { "1" }, knowIds2, reqModel.difficultLevel, reqModel.singleCount);
        for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : elasticYxyYxyQuesResourceLst) {
            // quesIdSet.add(elasticYxyYxyQuesResource.getId());
            questionModel = new QuestionModel();
            questionModel.quesId = elasticYxyYxyQuesResource.getId();
            List<Integer> knowIdList = elasticYxyYxyQuesResource.getKnowledgeIds();
            List<Integer> list = new ArrayList<Integer>(knowIdSet);
            list.retainAll(knowIdList);
            questionModel.know = list;
            questionModel.quesType = elasticYxyYxyQuesResource.getQuestionType();
            questions.add(questionModel);
            danxuan = danxuan + 1;

        }
        // 多选
        List<ElasticYxyYxyQuesResource> elasticYxyYxyQuesResourceLst2 = elasticQuesService
                .findQuesListByChapter(new String[] { "8" }, knowIds2, reqModel.difficultLevel, reqModel.moreCount);
        for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : elasticYxyYxyQuesResourceLst2) {
            // quesIdSet.add(elasticYxyYxyQuesResource.getId());
            questionModel = new QuestionModel();
            questionModel.quesId = elasticYxyYxyQuesResource.getId();
            List<Integer> knowIdList = elasticYxyYxyQuesResource.getKnowledgeIds();
            List<Integer> list = new ArrayList<Integer>(knowIdSet);
            list.retainAll(knowIdList);
            questionModel.know = list;
            questionModel.quesType = elasticYxyYxyQuesResource.getQuestionType();
            questions.add(questionModel);
            duoxuan = duoxuan + 1;
        }
        // 填空
        List<ElasticYxyYxyQuesResource> elasticYxyYxyQuesResourceLst3 = elasticQuesService
                .findQuesListByChapter(new String[] { "2" }, knowIds2, reqModel.difficultLevel, reqModel.blanksCount);
        for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : elasticYxyYxyQuesResourceLst3) {
            // quesIdSet.add(elasticYxyYxyQuesResource.getId());
            questionModel = new QuestionModel();
            questionModel.quesId = elasticYxyYxyQuesResource.getId();
            List<Integer> knowIdList = elasticYxyYxyQuesResource.getKnowledgeIds();
            List<Integer> list = new ArrayList<Integer>(knowIdSet);
            list.retainAll(knowIdList);
            questionModel.know = list;
            questionModel.quesType = elasticYxyYxyQuesResource.getQuestionType();
            questions.add(questionModel);
            tiankong = tiankong + 1;
        }
        // 解答
        List<ElasticYxyYxyQuesResource> elasticYxyYxyQuesResourceLst4 = elasticQuesService.findQuesListByChapter(
                new String[] { "1", "8", "2" }, knowIds2, reqModel.difficultLevel, reqModel.answerCount);
        for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : elasticYxyYxyQuesResourceLst4) {
            // quesIdSet.add(elasticYxyYxyQuesResource.getId());
            questionModel = new QuestionModel();
            questionModel.quesId = elasticYxyYxyQuesResource.getId();
            List<Integer> knowIdList = elasticYxyYxyQuesResource.getKnowledgeIds();
            List<Integer> list = new ArrayList<Integer>(knowIdSet);
            list.retainAll(knowIdList);
            questionModel.know = list;
            questionModel.quesType = elasticYxyYxyQuesResource.getQuestionType();
            questions.add(questionModel);
            jieda = jieda + 1;
        }
        RespModel model = new RespModel();
        model.question = questions;
        model.question = questions;
        model.danxuan = danxuan;
        model.duoxuan = duoxuan;
        model.tiankong = tiankong;
        model.jieda = jieda;
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {

        // 单选题的数量
        public Integer singleCount;
        // 多选题的数量
        public Integer moreCount;
        // 填空题的数量
        public Integer blanksCount;
        // 解答题的数量
        public Integer answerCount;
        // 题型难度
        public String difficultLevel;
        // 所选择的所有章节目录
        public List<Integer> bookContentIdList;
    }

    protected static class RespModel extends YxyApiRespModel {
        public List<QuestionModel> question;
        public Integer danxuan;
        public Integer duoxuan;
        public Integer tiankong;
        public Integer jieda;
    }

    protected static class QuestionModel {
        // 题id
        public String quesId;
        // 知识点id集合
        public List<Integer> know;
        // 分数
        public Integer point;
        // 题型
        public String quesType;
    }
}
